﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace ClockSync2
{
    public partial class Form1 : Form
    {
        RunningClock clock;
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            clock = new RunningClock();
            System.Threading.Thread t = new System.Threading.Thread(fun);
            System.Threading.Thread t2 = new System.Threading.Thread(gdfgdgdfggddf);

            clock.run();

            t.Start();
            t2.Start();

        }
        public void gdfgdgdfggddf()
        {
            int i = 0;
            //System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\ASoliD\Documents\Visual Studio 2012\Projects\ConsoleApplication3\times.txt");
            while (i < int.MaxValue)
            {
                this.label1.Text = utils.DateTimeAsString(clock.CurrentTime);
                System.Threading.Thread.Sleep(5);
                //file.WriteLine(utils.getTimeInMS((utils.DateTimeAsString(clock.CurrentTime).Split(':'))));
                i++;
            }
            // file.Close();

        }
        public void fun()
        {
            NeighbourTable ntb;

            //test ntb
            byte[] byteIps = {   (byte)'1',(byte)'0',(byte)'.',(byte)'1',(byte)'0',(byte)'0',(byte)'.',(byte)'1',(byte)'0',(byte)'2',(byte)'.',(byte)'3',(byte)':',(byte)'8',(byte)'0',(byte)'0',(byte)'4',(byte)';',
                                 (byte)'1',(byte)'0',(byte)'.',(byte)'1',(byte)'0',(byte)'0',(byte)'.',(byte)'1',(byte)'0',(byte)'2',(byte)'.',(byte)'4',(byte)':',(byte)'8',(byte)'0',(byte)'0',(byte)'2',(byte)';',
                                 (byte)'1',(byte)'0',(byte)'.',(byte)'1',(byte)'0',(byte)'0',(byte)'.',(byte)'1',(byte)'0',(byte)'2',(byte)'.',(byte)'5',(byte)':',(byte)'8',(byte)'0',(byte)'0',(byte)'3',(byte)';'};
            //Console.WriteLine("byteIps:" + System.Text.ASCIIEncoding.ASCII.GetString(byteIps));
            Console.WriteLine("the local NeighbourTable is:");
            ntb = new NeighbourTable(byteIps);
            int i = 0;
            foreach (Neighbour n in ntb.Neighbours)
            {
                n.syncPort = 9000 + i;
                Console.WriteLine(n.Address + ":" + n.Port);
                if (n.Address.Equals(IPAddress.Parse("10.100.102.3")))
                {
                    n.IsMaster = true;
                    Console.WriteLine("i set " + n.Address + " as Master");
                }
                i++;
            }
            Slave slave = new Slave(ntb, clock);
            slave.start();
        }
    }
}